package com.lh.tams.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lh.tams.domain.TamsModule;
import com.lh.tams.domain.TamsPerson;
import com.lh.tams.domain.TamsRole;
import com.lh.tams.service.ModuleService;
import com.lh.tams.service.system.RoleService;
import com.lh.tams.util.Constants;

@Controller
public class IndexController {
	private RoleService roleService;
	private ModuleService moduleService;

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}

	@RequestMapping("/index.adm")
	public String index(HttpSession session,HttpServletRequest request){
		TamsPerson person=(TamsPerson) session.getAttribute(Constants.ADMIN_SESSION_KEY);
		if(person==null)
			return "/pages/login";
		TamsRole role=person.getTamsRole();
		List<TamsModule> list=roleService.queryModule(role.getIdBgt());
		List<TamsModule> parents=moduleService.queryParent();
		Map<String, List<TamsModule>> map=new HashMap<String,List<TamsModule>>();
		List<TamsModule> supList=new ArrayList<TamsModule>();
		for(int i=0;i<parents.size();i++){
			List<TamsModule> subList=new ArrayList<TamsModule>();
			for(int j=0;j<list.size();j++){
				if(list.get(j).getTamsModule().getIdBgt()==parents.get(i).getIdBgt()){
					subList.add(list.get(j));
				}
			}
			if(subList.size()>0){
				map.put(parents.get(i).getIdBgt()+"", subList);
				supList.add(parents.get(i));
			}
		}
		request.setAttribute("supList", supList);
		request.setAttribute("map", map);
		
		//把当前的权限放到Session中
		List<String> limit=new ArrayList<String>();
		for(int i=0;i<list.size();i++){
			limit.add(list.get(i).getModuleUrl());
		}
		session.setAttribute(Constants.ADMIN_LIMIT, limit);
		
		return "/pages/index";
	}
}	
